package com.myfum.mobile.tetris.utils
{
	import com.myfum.mobile.tetris.Config;
	import com.myfum.mobile.tetris.events.ControlEvent;
	import com.myfum.mobile.tetris.views.TetrisStage;
	
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	
	import org.robotlegs.mvcs.Actor;
	
	[Event(name="rotate", type="com.myfum.mobile.tetris.events.ControlEvent")]
	[Event(name="moveLeft", type="com.myfum.mobile.tetris.events.ControlEvent")]
	[Event(name="moveRight", type="com.myfum.mobile.tetris.events.ControlEvent")]
	[Event(name="moveDown", type="com.myfum.mobile.tetris.events.ControlEvent")]
	[Event(name="drop", type="com.myfum.mobile.tetris.events.ControlEvent")]
	public class InteractionManager extends Actor
	{
		private static const thresholdX:int = Config.CUBE_SIZE;
		private static const thresholdY:int = Config.CUBE_SIZE;
		private var stage:Stage;
		private var tabX:Number;
		private var tabY:Number;
		private var tabPoint:Point = new Point();
		
		[Inject(name='TetrisStage')]
		public var tetrisStage:TetrisStage;
		
		public function InteractionManager()
		{
			super();
		}
		
		public function initialize(stage:Stage):void{
			this.stage = stage;
			
			addListeners();
		}
		
		private function addListeners():void{
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onTouchBegin);
		}
		
		private function onTab(e:TouchEvent):void{
			var evt:ControlEvent = new ControlEvent(ControlEvent.ROTATE);
			dispatch(evt);
		}
		
		private function onTouchBegin(e:MouseEvent):void{
			tabX = e.stageX;
			tabY = e.stageY;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onTouchMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, onTouchEnd);
			tabPoint.x = e.stageX;
			tabPoint.y = e.stageY;
		}
		
		private function onTouchMove(e:MouseEvent):void{
			if(e.stageY == 107374182.35){
				return;
			}
			if(e.stageY >= tabY + thresholdY*3){
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onTouchMove);
				tabY = e.stageY;
				dispatch(new ControlEvent(ControlEvent.DROP));
				return;
			}
			if(e.stageX > tabX + thresholdX){
				//move right
				dispatch(new ControlEvent(ControlEvent.MOVE_RIGHT));
				tabX = e.stageX;
			}else if(e.stageX < tabX - thresholdX){
				//move left
				dispatch(new ControlEvent(ControlEvent.MOVE_LEFT));
				tabX = e.stageX;
			}
			if(e.stageY > tabY + thresholdY){
				//move down
				dispatch(new ControlEvent(ControlEvent.MOVE_DOWN));
				tabY = e.stageY;
			}
		}
		
		private function onTouchEnd(e:MouseEvent):void{
			if(Math.abs(tabPoint.x - e.stageX) < Config.CUBE_SIZE/2 && Math.abs(tabPoint.y - e.stageY) < Config.CUBE_SIZE/2){
				var evt:ControlEvent = new ControlEvent(ControlEvent.ROTATE);
				dispatch(evt);
				stage.removeEventListener(MouseEvent.MOUSE_UP, onTouchEnd);
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onTouchMove);
			}else{
				stage.removeEventListener(MouseEvent.MOUSE_UP, onTouchEnd);
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onTouchMove);
			}
		}
	}
}